<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/finance.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
	</head>

	<body style="padding: 0px;">
		<!--<div id="toolbar" class="receipts_toolbar_style"></div>-->
		<div>
			<form id="ff" method="post">
				<div id="p" class="easyui-panel" style="width: 100%;">
					<table id="tt" style="width: 100%;">
						<!--<thead>
							<tr>
								<th field="productid" width="15%" align="center" editor="text">项目名称</th>
								<th field="listprice" width="15%" align="center" editor="{type:'datebox'}">起始时间</th>
								<th field="listprice" width="15%" align="center" editor="{type:'datebox'}">结束时间</th>
								<th field="unitcost" width="10%" align="center" editor="{type:'numberbox',options:{precision:1}}">应收金额</th>
								<th field="unitcost1" width="10%" align="center" editor="{type:'numberbox',options:{precision:1}}">实收金额</th>
								<th field="unitcost2" width="10%" align="center" editor="{type:'numberbox',options:{precision:1}}">留存金额</th>
								<th field="unitcost3" width="15%" align="center" editor="{type:'numberbox',options:{precision:1}}">缴费方式</th>
								<th field="attr1" width="13%" align="center" editor="text">备注</th>
							</tr>
						</thead>-->
					</table>
				</div>

				<table class="displaytable" style="width: 70%;margin-left: 15%;padding: 20px;">
					<tr>
						<td width="20%">人民币金额合计（小写）</td>
						<td width="30%"><input id="input_sum" style="border: none;"></input>
						</td>
						<td width="20%">人民币金额合计（大写）</td>
						<td width="30%"><input id="span_sum" style="border: none;"></td>
					</tr>
					<tr>
						<td>付款人：</td>
						<td>
							<input style="width:100%" class="easyui-textbox" type="text" name="evidence_ID" data-options="required:true"></input>
						</td>
						<td>收款人：</td>
						<td>
							<input style="width:100%" class="easyui-textbox" type="text" name="evidence_ID" data-options="required:true"></input>
						</td>
					</tr>
				</table>

				<div id="new_footer" style="margin-left: 23%;"></div>
			</form>
		</div>

		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 100%;height: 100%;" frameborder="0" scrolling="no"></iframe>
		</div>
	</body>
	<script type="text/javascript">
		var currid = AddFlag; //当前页面的ID
		var id = getID(); //获取上个页面传回的ID
		var currPage = 0; //当前的页数
		var MaxPage = 0;
		var MinPage = 1;
		var typeInfo = new Array(); //将typeInfo初始化为数组
		var _detailToolbarData;
		var AddDialog;
		var auth = "admin;" + hex_sha1("admin1");
		_detailToolbarData = AddDetailToolBar(23);

		var addToolbarData = [{
			"icon": "icon-save",
			"func": "save()",
			"functionname": "保存"
		}];
		$(document).ready(function(){
			if(id != AddFlag && id != undefined) {
				currid = id;
				loadRemote();
			}
		});
		if(id == undefined) {
			currid = AddFlag;
		}

		//		var tooldata = [{
		//			"icon": "icon-large-chart",
		//			"func": "more()",
		//			"functionname": "查看更多"
		//		}];
		///增加脚注
		getPageMaxAndMin();
		addFooter($("#new_footer"));
		///tooldata为新增的toolbar数据
		content = AddReceiptsToolBar(0, addToolbarData);
		$("#toolbar").append(content); /*在ID=toolbar的标记后面添加content中的内容*/
		function getPageMaxAndMin() {
			var _url = RemoteLinkAddress + "/market/tenant/getmaxminid.do";
			$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
				type: "get",
				url: _url,
				dataType: "json", //服务器返回的数据类型
				success: function(result) {
					console.log(result);
					if(result != null) {
						MaxPage = result.MAXID;
						MinPage = result.MINID;
						currPage = result.MINID + 1;
					}
				}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		function getRemoteData(_url) //统一获取远程的数据
		{
			$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
				type: "get",
				url: _url,
				dataType: "json", //服务器返回的数据类型
				success: function(result) {
					if(result != null) {
						currPage = result.ID; //表示传回的ID
						currid = result.TENANT_ID;
						$('#ff').form('load', result);
					}

				}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}
		$("#input_id").val(id);

		function print() {
			var pLocation = "";
			if(currid == AddFlag) {
				$.messager.alert("错误", "当前页面为新增页面不能打印");
				return;
			} else {
				pLocation = "OriginalOrderPrint.html?id=" + currid;
				window.open(pLocation);
			}
		}

		function newdata() {
			currid = AddFlag;
			currPage = 0;
			$('#ff').form('reset');
		}

		function frontpage() {
			if(currPage >= MinPage) {
				var _url = RemoteLinkAddress + '/logistics/tenant/pre.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert("提示", "没有上张");
			}
		}

		function nextpage() {
			if(currPage <= MaxPage) {
				var _url = RemoteLinkAddress + '/logistics/tenant/next.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert(" 提示", "没有下张");
			}

		}

		function searchmore() {
			AddTabIn("收费查询", parent.$('#tabs'), "UI/Market/Receipt/receiptSearch.html");
		}

		function loadRemote() { //载入服务器上的数据，用于修改以及查看更多的时候调用，根据id是否等于-1来判断
			var _url = RemoteLinkAddress + '/market/tenant/findbyid.do?TENANT_ID=' + currid;
			getRemoteData(_url);
		}
		var projectName = [{
			"value": "1",
			"text": "租金"
		}, {
			"value": "2",
			"text": "物业费"
		}, {
			"value": "3",
			"text": "滞纳金"
		}];
		var PaymentMethod = [{
			"value": "1",
			"text": "现金"
		}, {
			"value": "2",
			"text": "预交"
		}, {
			"value": "3",
			"text": "刷卡"
		}];
		var toolbar = [{
			text: '新增',
			iconCls: 'icon-add',
			handler: function() {
				$('#tt').edatagrid('addRow');
			}
		}, {
			text: '取消',
			iconCls: 'icon-cancel',
			handler: function() {
				$('#tt').edatagrid('cancelRow')
			}
		}, {
			text: '保存',
			iconCls: 'icon-save',
			handler: function() {
				$('#tt').edatagrid('saveRow')
			}
		}, {
			text: '删除',
			iconCls: 'icon-remove',
			handler: function() {
				$('#tt').edatagrid('destroyRow')
			}
		}];
		GetTable();

		function cls() {
			$('#ff').form('reset');
		}
		function GetTable() {
			$('#tt').edatagrid({
				height: 350,
				toolbar: _detailToolbarData,
				pagination: 'true',
				rownumbers: 'true',
				singleSelect: true,
				fitColumns: true,
				autoSave: true,
				columns: [
					[{
						field: 'ID',
						title: 'ID',
						width: '0%',
						align: 'center',
					}, {
						field: 'CHARGENAME',
						title: '费用名称',
						width: '25%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: "NAME",
								textField: "NAME",
								url: RemoteLinkAddress + "/market/chargetype/listall.do",
								required: true,
							}
						}
					}, {
						field: 'CHARGETYPE',
						title: '费用类型',
						width: '24%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: "CHARGETYPE",
								textField: "CHARGETYPE",
								editable: false,
								required: true,
								data: [{
									"CHARGETYPE": "收费",
									"CHARGETYPE": "收费"
								}, {
									"CHARGETYPE": "退费",
									"CHARGETYPE": "退费"
								}]
							}
						}
					}, {
						field: 'EVIDENCE_ID',
						title: '交易凭证号',
						width: '24%',
						align: 'center',
						editor: {
							type: 'textbox',
						}
					}, {
						field: 'SUM',
						title: '金额',
						width: '24%',
						align: 'center',
						editor: {
							type: 'numberbox',
						},
						formatter: function(value, row, index) {
							if(value) {
								return "￥" + value;
							}
						}
					}]
				],
				onEndEdit: function(index, row, changes) { //在编辑完成或，执行该函数
					CalSum();
				}
			});
		}

		$("#tt").datagrid('hideColumn', 'ID');

		var dialogWidth = 400;
		var dialogHeight = 300;
		var AddDialog;

		function getData() {
			var totalData = JSON.parse(localStorage.shortTermContractData);
//			totalData.SUM = $("#input_sum").val();
			totalData.rowsCharge = new Array();
			var dataGridRows = $("#tt").datagrid("getRows");
			for(var i = 0; i < dataGridRows.length; i++) {
				var _index = i;
				totalData.rowsCharge[_index] = new Object();
				totalData.rowsCharge[_index].CHARGENAME = dataGridRows[i].CHARGENAME;
				totalData.rowsCharge[_index].CHARGETYPE = dataGridRows[i].CHARGETYPE;
				totalData.rowsCharge[_index].EVIDENCE_ID = dataGridRows[i].EVIDENCE_ID;
				totalData.rowsCharge[_index].SUM = dataGridRows[i].SUM;
			}
			localStorage.setItem("shortTermContractData",JSON.stringify(totalData));
			$.messager.alert("提示","保存成功!");
			console.log(totalData);
//			localStorage.shortTermContractData = JSON.stringify(totalData);
		}

		function CalSum() {
			var _num = 0;
			var dataGridRows = $("#tt").datagrid("getRows");
			console.log(dataGridRows);
			for(var i = 0; i < dataGridRows.length; i++) {
				dataGridRows[i].UNIT_SUM = parseInt(dataGridRows[i].UNIT_SUM);
				if(typeof(dataGridRows[i].CHARGETYPE) != "undefined") {
					_num += dataGridRows[i].UNIT_SUM;
				}
			}
			var num = changeMoneyToChinese(_num);
			$("#input_sum").val(_num);
			$("#span_sum").val(num);
		}

		function save() {
			getData();
		}
	</script>

</html>